<?php

/**
 * Desc
 * @description Holp You Do Good But Not Evil
 * @copyright   Copyright 2014-2015 <ycchen@iwshop.cn>
 * @license     LGPL (http://www.gnu.org/licenses/lgpl.html)
 * @author      Chenyong Cai <ycchen@iwshop.cn>
 * @package     Wshop
 * @link        http://www.iwshop.cn
 */
class Index extends Controller {

    /**
     * 
     * @param type $ControllerName
     * @param type $Action
     * @param type $QueryString
     */
    public function __construct($ControllerName, $Action, $QueryString) {
        parent::__construct($ControllerName, $Action, $QueryString);
    }

    /**
     * 店铺首页
     * @param type $Q
     */
    public function index($Q) {

        $this->loadModel('User');
        $this->loadModel('WechatSdk');
        $this->loadModel('Product');
        $this->loadModel('Carts');
        $openId = $this->getOpenId();
        
        if(!Controller::inWechat() && !$this->debug){

            $this->show('./index/error.tpl');
            die(0);
        }
        //error_log("==============$openId==================".$openId);
        // 微信注册
        $this->User->wechatAutoReg($openId);
        
        $this->Smarty->cache_lifetime = 0;

        if (!$this->isCached()) {
            
            $topCats = $this->Product->getCatList(0);
            if($topCats){
               $this->assign('catId', $topCats[0]['cat_id']);
             }
          
            $openid = $openId;//$this->getOpenId();
            $uinfo = $this->User->getUserInfo($openid);
            $uid = $uinfo['uid'];

           $productList = $this->Carts->get_cart_products($uid);
           $count = 0; 
           $total = 0;
           if($productList){

              foreach($productList as   $k => $v) {     
                 $count += $v['product_quantity']; 
                 $productSpecs =  $this->Product->getProductSpecs($v['product_id']);
                 if($productSpecs){
                    $total=$total+$v['product_quantity']*$productSpecs[0]['sale_price'];
                 }
     
             } 

         
             foreach($topCats as   $c_k => $c_v) {  

                foreach($productList as   $p_k => $p_v) {  

                  if($c_v['cat_id'] == $p_v['product_cat']){

                       $topCats[$c_k]['count'] = $topCats[$c_k]['count']+$p_v['product_quantity'];

                  }
                }
             } 
          }
        // error_log("======cat===".json_encode($topCats));
         $this->assign('topcat', $topCats);
         $this->assign('count', $count);
         $this->assign('total', $total);
   
        }
        $this->show();
    }
    
    
    public final function ajax_list_item($Query){
        $this->loadModel('Product');
        $openid = $this->getOpenId();
        $this->loadModel('Carts');
        $this->loadModel('User');
        $uinfo = $this->User->getUserInfo($openid);
        $uid = $uinfo['uid'];
     
        
        if (isset($Query->id)) {
            $this->cacheId = intval($Query->id);
            
            $products = $this->Product->getNewEst($this->cacheId,50);
            
            //必须要添加一个规格
            foreach ($products as   $key => $val) {
               $productSpecs =   $this->Product->getProductSpecs($val['product_id']);
               if($productSpecs){
                  $products[$key]['pinfo'] = $productSpecs[0];
               }
                $products[$key]['product_quantity'] = 0;
                error_log(json_encode($products));
            }
            $count = 0; 
            $totalMoney = 0;
            $productList = $this->Carts->get_cart_products($uid);
            if($productList){
                 foreach($products as   $key => $val) {
                    
                      foreach($productList as   $k => $v) {
                          
                          if($val['product_id'] == $v['product_id']){
                          
                             $products[$key]['product_quantity'] = $v['product_quantity'];
                             $totalMoney = $totalMoney+$v['product_quantity']*$val['sale_price'];
                          }
                          
                      }
                 }
                  
                //计算购物车总数
                 foreach($productList as   $k => $v) {     
                   $count = $count + $v['product_quantity'];
                   
                 }  
                 
            }
          

            $this->assign('count', $count);
            $this->assign('total', $totalMoney);
            $this->assign('products', $products);
            $this->show('./index/ajax_list_item.tpl');
                    // 分类下面无子分类
             
        }
   }
   
  public function ajaxGetCartProducts(){
  
     $this->loadModel('Carts');
     $this->loadModel('User');
     $this->loadModel('Product');
     $openid = $this->getOpenId();
     $uinfo = $this->User->getUserInfo($openid);
     $uid = $uinfo['uid'];
     $totalMoney = 0;
     $productList = $this->Carts->get_cart_products($uid);
     foreach ($productList as   $key => $val) {
          $productSpecs = $this->Product->getProductSpecs($val['product_id']);
          if($productSpecs){
              $productList[$key]['pinfo'] = $productSpecs[0];
              $totalMoney = $totalMoney+$val['product_quantity']*$productSpecs[0]['sale_price'];
              
          }
      }
       $count = 0; 
      foreach($productList as   $k => $v) {     
          $count += $v['product_quantity'];      
      } 
     $this->assign('product_list', $productList);
     $this->assign('count', $count);
     $this->assign('total', $totalMoney);
     $this->show('./index/ajax_cart_item.tpl');
     
  }

   public function ajaxRemoveProduct(){

       $this->loadModel('Carts');
       $this->loadModel('User');
       $this->loadModel('Product');

       $productId = $_POST['product_id'];
       $specId = $_POST['spec_id'];

       
       $openid = $this->getOpenId();
   
       $uinfo = $this->User->getUserInfo($openid);
       $uid = $uinfo['uid'];
       $this->Carts->remove_product($uid,$productId,$specId);
       $productList = $this->Carts->get_cart_products($uid);
       foreach ($productList as   $key => $val) {
          $productSpecs = $this->Product->getProductSpecs($val['product_id']);
          if($productSpecs){
              $productList[$key]['pinfo'] = $productSpecs[0];
              $totalMoney = $totalMoney+$val['product_quantity']*$productSpecs[0]['sale_price'];
              
          }
      }
      $count = 0; 
      foreach($productList as   $k => $v) {     
          $count += $v['product_quantity'];      
      } 
      //error_log("=============productList==========".json_encode(count($productList)));
     $this->assign('product_list', $productList);
     $this->assign('count', $count);
     $this->show('./index/ajax_cart_item.tpl');
  }
  
  public function ajaxRemoveProductUPdateData(){
  
     $this->loadModel('Carts');
     $this->loadModel('User');
     $this->loadModel('Product');
     $openid = $this->getOpenId();
     $uinfo = $this->User->getUserInfo($openid);
     $uid = $uinfo['uid'];
     $totalMoney = 0;
     
     $productId = $_POST['pid'];
     $specId = $_POST['sid'];
     $this->Carts->remove_product($uid,$productId,$specId);
     
     $productList = $this->Carts->get_cart_products($uid);
     foreach ($productList as   $key => $val) {
          $productSpecs = $this->Product->getProductSpecs($val['product_id']);
          if($productSpecs){
              $productList[$key]['pinfo'] = $productSpecs[0];
              $totalMoney = $totalMoney+$val['product_quantity']*$productSpecs[0]['sale_price'];
              
          }
      }
     $count = 0; 
     foreach($productList as   $k => $v) {     
          $count += $v['product_quantity'];      
     }
     
    $topCats = $this->Product->getCatList(0);
    foreach($topCats as   $c_k => $c_v) {  

                foreach($productList as   $p_k => $p_v) {  

                  if($c_v['cat_id'] == $p_v['product_cat']){

                       $topCats[$c_k]['count'] = $topCats[$c_k]['count']+$p_v['product_quantity'];

                  }
                }
         } 
     
     $data =  array(
           'count' => $count,
           'total' =>$totalMoney,
           'topCats' =>$topCats
     );
    $this->echoJson($data);
     
  }

}
